package com.common.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.sf.ehcache.util.PropertyUtil;

/**
 * 读取 Constant.properties 文件
* @author 	zh
* @date 2018年1月23日
 */
public class ConstantPropUtil {

	private static final Logger logger = LoggerFactory.getLogger(ConstantPropUtil.class);
	private static Properties props;
    static{
        loadProps();
    }
    synchronized static private void loadProps(){
        logger.info("开始加载constant.properties文件内容.......");
        props = new Properties();
        InputStream in = null;
        try {
        	//第一种，通过类加载器进行获取properties文件流
            in = PropertyUtil.class.getClassLoader().getResourceAsStream(
            		"config"+ File.separator+"env"+File.separator+"constant.properties");
//            props.load(in);
            props.load(new InputStreamReader(in, "UTF-8")); 
        } catch (FileNotFoundException e) {
            logger.error("constant.properties文件未找到");
        } catch (IOException e) {
            logger.error("出现IOException");
        } finally {
            try {
                if(null != in) {
                    in.close();
                }
            } catch (IOException e) {
            }
        }
        logger.info("加载constant.properties文件内容完成...........");
    }
    public static String getProperty(String key){
        if(null == props) {
            loadProps();
        }
        return props.getProperty(key);
    }

    public static String getProperty(String key, String defaultValue) {
        if(null == props) {
            loadProps();
        }
        return props.getProperty(key, defaultValue);
    }
}
